"""
Case Type   : cm
Case Name   : cm_ctl start启动所有数据库服务，仲裁模式参数验证
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、所有节点存活，指定cm_server的仲裁模式为多数派
    3、获取cm_server的仲裁模式
    4、所有节点存活，指定cm_server法人仲裁模式为少数派
    5、获取cm_server法人仲裁模式
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、执行成功
    3、获取成功，结果为majority
    4、执行成功
    5、获取成功，结果为majority，cm会自行将仲裁模式改为多数派
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '----step2:所有节点存活，指定cm_server的仲裁模式为多数派; expect:执行成功----'
        self.log.info(text)

        cm_set1 = self.comsh.exec_cm_ctl('start', '--cm_arbitration_mode=majority')
        self.log.info(cm_set1)
        self.assertIn(self.constant.cm_start_success_msg, cm_set1, '执行失败')

        text = '----step3:获取cm_server的仲裁模式  expect:获取成功，结果为majority----'
        self.log.info(text)

        cm_res1 = self.comsh.exec_cm_ctl('get', '--cm_arbitration_mode')
        self.log.info(cm_res1)
        self.assertIn('cm_arbitration_mode=MAJORITY', cm_res1, '获取结果异常')

        text = '----step4:所有节点存活，指定cm_server的仲裁模式为少数派; expect:执行成功----'
        self.log.info(text)

        cm_set2 = self.comsh.exec_cm_ctl('start', '--cm_arbitration_mode=minority')
        self.log.info(cm_set2)
        self.assertIn(self.constant.cm_start_success_msg, cm_set2, '执行失败')

        text = '----step5:获取cm_server的仲裁模式  expect:获取成功，结果为majority----'
        self.log.info(text)

        cm_res1 = self.comsh.exec_cm_ctl('get', '--cm_arbitration_mode')
        self.log.info(cm_res1)
        self.assertIn('cm_arbitration_mode=MAJORITY', cm_res1, '获取结果异常')

    def tearDown(self):
        self.log.info('-----Teardowm:恢复初始集群状态-----')

        start = self.comsh.exec_cm_ctl('start')
        self.log.info(start)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        p_status = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        if 'Primary' in p_status:
            self.log.info('---集群状态正常，无需恢复为初始状态---')
        else:
            recovery = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
            self.log.info(recovery)

            self.assertIn(self.constant.cm_switchover_success_msg, recovery,
                          '切换失败')

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '状态异常')
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '状态异常')
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '状态异常')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
